﻿//////////////////////////////////////////////////////////////////////////////
//
// Copyright © 1998-2024 Glodon Company Limited.
//
// Licensed under the MIT License
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the “Software”),
// to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
// sell copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
//////////////////////////////////////////////////////////////////////////////
#include "QGroupObjectTreeWidget.h"
#include "IUIConfiguration.h"
#include "GbmpUiPlatformCommandIds.h"
// QT
#include <QMenu>
#include <QMouseEvent>
// last include header file.
#include "EnableCompileWarning_The_LAST_IncludeInCpp.h"

using namespace gcmp;

QGroupObjectTreeWidget::QGroupObjectTreeWidget(QWidget* parent) :
    QTreeWidget(parent)
{
    setSelectionMode(QAbstractItemView::ExtendedSelection);
    setDragEnabled(true);
    setStyleSheet("QCADTreeWidget::item:selected{ background-color: rgb(51, 153, 255)} QCADTreeWidget{ font-family: \"Microsoft YaHei\";}");
    setFocusPolicy(Qt::ClickFocus);

    m_treeMenu = NEW_AS_QT_CHILD(QMenu, this);
    if (m_treeMenu)
    {
        m_treeMenu->setObjectName("treeMenu");
    }
    
    setFocusPolicy(Qt::ClickFocus);
}

QGroupObjectTreeWidget::~QGroupObjectTreeWidget()
{
}

void QGroupObjectTreeWidget::UpdateView(gcmp::IDocument* pDoc)
{
    DBG_WARN_AND_RETURN_VOID_UNLESS(pDoc, L"invalid pDoc",L"GDMPLab",L"2024-03-30");
    QFamilyTreeWidgetUtil::UpdateGroupObjectItems(this, pDoc, m_icons);

    if (topLevelItemCount() > 0)
        setVisible(true);
    else
        setVisible(false);
}

void QGroupObjectTreeWidget::mousePressEvent(QMouseEvent *event)
{
    if (event->button() == Qt::RightButton)
    {
        QTreeWidgetItem *item = this->itemAt(event->pos());
        if (item != nullptr)
        {
            if (!item->isSelected())
            {
                this->clearSelection();
                this->setCurrentItem(item);
                item->setSelected(true);
            }

            if (m_treeMenu != nullptr)
            {
                QList<QTreeWidgetItem*> selectedItem = selectedItems();
                UpdateGroupObjectTreeItemContextMenu(selectedItem);
                if (!m_treeMenu->isEmpty())
                {
                    m_treeMenu->exec(QCursor::pos()); //在当前鼠标位置显示
                }
            }
        }
    }

    QTreeWidget::mousePressEvent(event);
}

void QGroupObjectTreeWidget::UpdateGroupObjectTreeItemContextMenu(const QList<QTreeWidgetItem*>& selectedItem)
{
    if (!m_treeMenu)
        return;

    m_treeMenu->clear();
    if (selectedItem.size() == 1)
    {
        // 单选
        int nodeType = selectedItem[0]->data(0, GROUP_USER_DATA_ROLE).toInt();
        if (nodeType == TWNT_GROUP_OBJECT_ROOT)
        {
            QAction* pCollapseItemsAction = m_treeMenu->addAction(QString::fromStdWString(GBMP_TR(L"全部折叠(&F)...")));
            connect(pCollapseItemsAction, SIGNAL(triggered(bool)), this, SLOT(collapseAll()));

            QAction* pExpandItemsAction = m_treeMenu->addAction(QString::fromStdWString(GBMP_TR(L"全部展开(&E)...")));
            connect(pExpandItemsAction, SIGNAL(triggered(bool)), this, SLOT(expandAll()));
        }
        else if (nodeType == TWNT_GROUP_MODEL)
        {
            QAction* pCreateFamilyInstance = m_treeMenu->addAction(QString::fromStdWString(GBMP_TR(L"创建组")));
            connect(pCreateFamilyInstance, SIGNAL(triggered(bool)), this, SLOT(CreateGroup()));

            QAction* pRemoveAction = m_treeMenu->addAction(QString::fromStdWString(GBMP_TR(L"删除(&D)")));
            connect(pRemoveAction, SIGNAL(triggered(bool)), this, SLOT(DeleteGroupType()));
        }
    }
}

void QGroupObjectTreeWidget::CreateGroup()
{
    QFamilyTreeWidgetUtil::TrigerGroupAction(currentItem(), ID_CMD_CREATE_GROUP_BY_TYPE, ID_CMD_CREATE_GROUP_BY_TYPE);
}

void QGroupObjectTreeWidget::DeleteGroupType()
{
    QFamilyTreeWidgetUtil::TrigerGroupAction(currentItem(), ID_CMD_CREATE_GROUP_BY_TYPE, ID_CMD_DELETE_GROUP_TYPE);
}
